package cn.exrick.manager.service.impl;

import cn.exrick.common.exception.XmallException;
import cn.exrick.common.pojo.ZTreeNode;
import cn.exrick.manager.dto.DtoUtil;
import cn.exrick.manager.dto.TreeNode;
import cn.exrick.manager.mapper.TbItemCatMapper;
import cn.exrick.manager.pojo.TbItemCat;
import cn.exrick.manager.pojo.TbItemCatExample;
import cn.exrick.manager.service.ItemCatService;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

@Service
public class ItemCatServiceImpl implements ItemCatService {

    @Autowired
    private TbItemCatMapper tbItemCatMapper;

    @Override
    public TbItemCat getItemCatById(Long id) {

        TbItemCat tbItemCat=tbItemCatMapper.selectByPrimaryKey(id);
        if(tbItemCat==null){
            throw new XmallException("通过id获取商品分类失败");
        }
        return tbItemCat;
    }

    @Override
    public List<ZTreeNode> getItemCatList(int parentId) {

        TbItemCatExample example=new TbItemCatExample();
        TbItemCatExample.Criteria criteria=example.createCriteria();
        //排序
        example.setOrderByClause("sort_order");
        //条件查询
        criteria.andParentIdEqualTo(new Long(parentId));
        List<TbItemCat> list = tbItemCatMapper.selectByExample(example);

        //转换成ZtreeNode
        List<ZTreeNode> resultList=new ArrayList<>();

        for(TbItemCat tbItemCat:list){

            ZTreeNode node= DtoUtil.TbItemCat2ZTreeNode(tbItemCat);

            resultList.add(node);
        }

        return resultList;
    }

    @Override
    public int addItemCat(TbItemCat tbItemCat) {

        if(tbItemCat.getParentId()==0){
            //根节点
            tbItemCat.setSortOrder(0);
            tbItemCat.setStatus(1);
            tbItemCat.setCreated(new Date());
            tbItemCat.setUpdated(new Date());
        }else{
            TbItemCat parent=tbItemCatMapper.selectByPrimaryKey(tbItemCat.getParentId());
            tbItemCat.setSortOrder(0);
            tbItemCat.setStatus(1);
            tbItemCat.setCreated(new Date());
            tbItemCat.setUpdated(new Date());
        }


        if(tbItemCatMapper.insert(tbItemCat)!=1){
            throw new XmallException("添加商品分类失败");
        }
        return 1;
    }

    @Override
    public int updateItemCat(TbItemCat tbItemCat) {

        TbItemCat old=getItemCatById(tbItemCat.getId());
        tbItemCat.setCreated(old.getCreated());
        tbItemCat.setUpdated(new Date());

        if(tbItemCatMapper.updateByPrimaryKey(tbItemCat)!=1){
            throw new XmallException("添加商品分类失败");
        }
        return 1;
    }

    @Override
    public void deleteItemCat(Long id) {

        deleteZTree(id);
    }

    @Override
    public void deleteZTree(Long id) {

        //查询该节点所有子节点
        List<ZTreeNode> node=getItemCatList(Math.toIntExact(id));
        if(node.size()>0){
            //如果有子节点，遍历子节点
            for(int i=0;i<node.size();i++){
                deleteItemCat((long) node.get(i).getId());
            }
        }
        //没有子节点直接删除
        if(tbItemCatMapper.deleteByPrimaryKey(id)!=1){
            throw new XmallException("删除商品分类失败");
        }
    }

    @Override
    public List<ZTreeNode> getItemCatTree(String parentId) {
        if(StringUtils.isBlank(parentId)){
            parentId = "0";
        }

        TbItemCatExample example=new TbItemCatExample();
        TbItemCatExample.Criteria criteria=example.createCriteria();
        example.setOrderByClause("sort_order");
        criteria.andParentIdEqualTo(new Long(parentId));
        List<TbItemCat> list = tbItemCatMapper.selectByExample(example);
        list.sort(Comparator.comparing(TbItemCat::getSortOrder));
        List<ZTreeNode> resultList=new ArrayList<>();

        for(TbItemCat tbItemCat:list){
            ZTreeNode node= DtoUtil.TbItemCat2ZTreeNode(tbItemCat);
            if(tbItemCat.getIsParent()){
                List<ZTreeNode> childList = getItemCatTree(tbItemCat.getId().toString());
                if(CollectionUtils.isNotEmpty(childList)){
                    node.setChildren(childList);
                }
            }
            resultList.add(node);
        }
        return resultList;
    }

    @Override
    public List<String> getChildCatIds(Long parentId) {
        Set<String> result = new HashSet<>();
        result.add(parentId.toString());
        TbItemCatExample example=new TbItemCatExample();
        TbItemCatExample.Criteria criteria=example.createCriteria();
        criteria.andParentIdEqualTo(parentId);
        List<TbItemCat> list = tbItemCatMapper.selectByExample(example);
        if(CollectionUtils.isNotEmpty(list)){
            for(TbItemCat itemCat : list){
                Long childId = itemCat.getId();
                result.add(childId.toString());
                if(itemCat.getIsParent()){
                    result.addAll(getChildCatIds(childId));
                }
            }
        }
        return new ArrayList<>(result);
    }


    @Override
    public List<TreeNode> getItemCatTreeNodes(Long parentId,Long checkedId) {
        if(parentId == null){
            parentId = 0L;
        }

        TbItemCatExample example=new TbItemCatExample();
        TbItemCatExample.Criteria criteria=example.createCriteria();
        example.setOrderByClause("sort_order");
        criteria.andParentIdEqualTo(parentId);
        List<TbItemCat> list = tbItemCatMapper.selectByExample(example);
        list.sort(Comparator.comparing(TbItemCat::getSortOrder));
        List<TreeNode> resultList=new ArrayList<>();

        for(TbItemCat tbItemCat:list){
            TreeNode node= DtoUtil.TbItemCat2TreeNode(tbItemCat);
            if(tbItemCat.getId().longValue() == checkedId){
                node.getState().setSelected(true);
            }

            if(tbItemCat.getIsParent()){
                List<String> childCatIds = getChildCatIds(tbItemCat.getId());
                if(CollectionUtils.isNotEmpty(childCatIds) && childCatIds.contains(String.valueOf(checkedId))){
                    node.getState().setExpanded(true);
                }

                List<TreeNode> childList = getItemCatTreeNodes(tbItemCat.getId(),checkedId);
                if(CollectionUtils.isNotEmpty(childList)){
                    node.setNodes(childList);
                }
            }
            resultList.add(node);
        }
        return resultList;
    }
}
